//提交订单
var app_url=location.origin;
var Customer_id="";
var PlaceOrder=false;//代下单
var CustomerID=$.cookie("CustomerID");
var CurrentSet=$.cookie("CurrentSet");
var UserID=getcookie("UserID");
var CurrentRMB=false;
var CurrentUSD=false;
var cart_id=0;
(function(){
	var conformOrder={
		init:function(){
			args = GetUrlParms();				
			if(args["type"]!=undefined){						
				 TYPE = args["type"];//产品ID					
			} 
			if(args["cid"]!=undefined){
				 Customer_id = args["cid"];
				 PlaceOrder=true;
			}
            if(args["cart"]!=undefined){
                cart_id=args["cart"]
            }
			this.bindEvents();
		},
		bindEvents:function(){
			var self=this;
			if(CurrentSet==1){
				CurrentRMB=true;
			}else if(CurrentSet==0){
				CurrentRMB=true;
				CurrentUSD=true
			}else if(CurrentSet==2){
				CurrentUSD=true
			}
			if(TYPE==1 && PlaceOrder==false){
				Array.prototype.sum = function (){
				    return this.reduce(function (partial, value){
				        return partial + value;
				    })
				};
				if(PARAMS.ConfirmUSD && PARAMS.ConfirmPrice){
					//console.log(PARAMS)
					var USD_Array= PARAMS.ConfirmUSD;
					var RMB_Array= PARAMS.ConfirmPrice;
					var usd_sum = USD_Array.sum();
					var rmb_sum=RMB_Array.sum();
					var $html="";
					if(RMBprice_td==true && CurrentRMB==true){
						$html+='<div><span class="order_rmb_box">应付金额￥：<b class="stars_tip" id="remark_Amount_rmb">'+formatRMBPrice(rmb_sum)+'</b></span>';
						$html+='<span class="order_rmb_box">优惠后金额￥：<b class="stars_tip" id="remark_discount_Amount_rmb">'+formatRMBPrice(rmb_sum*Discounts)+'</b></span></div>';
					}
	                if(USDprice_td==true && CurrentUSD==true){
	                	$html+='<div><span class="order_usd_box">应付金额$：<b class="stars_tip" id="remark_Amount_usd">'+formatUSDPrice(usd_sum)+'</b></span>';
	                	$html+='<span class="order_usd_box">优惠后金额$：<b class="stars_tip" id="remark_discount_Amount_usd">'+formatUSDPrice(usd_sum*Discounts)+'</b></span></div>';
	                }
	                $("#price_discount_box").html($html);
	            }
			}


            if(PlaceOrder==false){
                Array.prototype.sum = function (){
                    return this.reduce(function (partial, value){
                        return partial + value;
                    })
                };

                if(PARAMS.PriceUSD && PARAMS.PriceRMB){
                	if(TYPE==3){

						var USD_Array= Number(PARAMS.PriceUSD);
						var RMB_Array= Number(PARAMS.PriceRMB);
					}else if(TYPE==2){
                        var USD_Array= Number(PARAMS.PriceUSD)*Number(PARAMS.couNum);
                        var RMB_Array= Number(PARAMS.PriceRMB)*Number(PARAMS.couNum);
					}
                    var $html="";
                    if(RMBprice_td==true && CurrentRMB==true){
                        $html+='<div><span class="order_rmb_box CurrentRMB">应付金额￥：<b class="stars_tip" id="remark_Amount_rmb">'+formatRMBPrice(RMB_Array)+'</b></span>';
                        $html+='<span class="order_rmb_box CurrentRMB">优惠后金额￥：<b class="stars_tip" id="remark_discount_Amount_rmb">'+formatRMBPrice(RMB_Array*Discounts)+'</b></span></div>';
                    }
                    if(USDprice_td==true && CurrentUSD==true){
                        $html+='<div><span class="order_usd_box CurrentUSD">应付金额$：<b class="stars_tip" id="remark_Amount_usd">'+formatUSDPrice(USD_Array)+'</b></span>';
                        $html+='<span class="order_usd_box CurrentUSD">优惠后金额$：<b class="stars_tip" id="remark_discount_Amount_usd">'+formatUSDPrice(USD_Array*Discounts)+'</b></span></div>';
                    }
                    $("#price_discount_box").html($html);
                }
            }



            $("#submit_order_btn").on("click", function () {
            	if($("#remarks").val()==""){
            		myApp.alert(_("请输入备注！"))
            	}else{
            		self.SubmitOrder();
            	}               
            });
            $("#reset_btn").on("click", function () {
                $("#remarks").val("");
            });   
            //继续逛逛
            $("#go_shop").on("click",function(){
            	$("#order_tip").hide();
            	if(PlaceOrder==true){
					if(PARAMS.form=="stocks"){
                        var linkUrl="/Team/WhiteDias?team=3&cid="+Customer_id+'&userName='+NullFormat(getUrlParam('userName'))
                        if(APP){
                            YiYi_APP.finish();
                        }else{
                            location.href=linkUrl;
                        }
					}else{
                        var urlArray={1:"WhiteDias?team=3&cid=",2:"WhiteParcels?team=3&cid=",3:"Ring?team=3&cid=",4:"Gem?team=3&cid=",5:"Emerald?team=3&cid=",6:"PrimeGold?team=3&cid=",7:"InlaySettings?team=3&cid="};
                        var linkUrl="/Team/"+urlArray[TYPE]+Customer_id+'&userName='+NullFormat(getUrlParam('userName'));
                        if(APP){
                            YiYi_APP.finish();
                        }else{
                            location.href=linkUrl;
                        }					}
				}else{
					if(PARAMS.form=="cart"){
						var urlArray={1:"DiaCart",2:"ParcelCart",3:"RingCart",4:"GemCart",5:"EmeraldCart",6:"PrimeGoldCart",7:"InlaySettingsCart"};
					}else{
						var urlArray={1:"WhiteDias?team=0",2:"WhiteParcels?team=0",3:"Ring?team=0",4:"Gem?team=0",5:"Emerald?team=0",6:"PrimeGold?team=0",7:"InlaySettings?team=0"};
					}
					var linkUrl="/Customer/"+urlArray[TYPE];
					if(APP){
						YiYi_APP.finish();
					}else{
						location.href=linkUrl;
					}
				}
            });
             //查看订单
            $("#go_order").on("click",function(){
            	$("#order_tip").hide();
            	if(PlaceOrder==true){
                    var urlArray={1:"Order",2:"ParcelOrder?type=parcels",3:"RingOrder?type=rings",4:"GemOrder?type=gems",5:"EmeraldOrder?type=emeralds",7:"InlaySettingsOrder"};
                    var linkUrl="/Team/"+urlArray[TYPE];
                    if(APP){
                        var appJson={};
                        appJson.title=_("订单");
                        appJson.function_menu="order";
                        appJson.goods_type=TYPE;
                        appJson.url=app_url+linkUrl;
                        YiYi_APP.showExistingPage(JSON.stringify(appJson));
                    }else{
                        location.href=linkUrl;
                    }
				}else{
					var urlArray={1:"DiaOrder",2:"ParcelOrder",3:"RingOrder",4:"GemOrder",5:"EmeraldOrder",6:"PrimeGoldOrder",7:"InlaySettingsOrder"};
					var linkUrl="/Customer/"+urlArray[TYPE];
					if(APP){
						var appJson={};
						appJson.title=_("订单");
						appJson.function_menu="order";
						appJson.goods_type=TYPE;
						appJson.url=app_url+linkUrl;
						YiYi_APP.showExistingPage(JSON.stringify(appJson));
					}else{
						location.href=linkUrl;
					}
				}
            });
		},
		 //提交订单（单个或多个立即定购）
        SubmitOrder:function() {
        	myApp.showPreloader(_("正在提交中，请耐心等候..."));
            var requestOrder = {};
			   var confirmId= PARAMS.orderID.join(',');
           	if(PlaceOrder==true){
           		requestOrder.CustomerID=Customer_id;
           		requestOrder.OrderType=2;//代下单
				if(TYPE!=1){
					requestOrder.UserID=UserID;
				}
           	}else{
           		requestOrder.CustomerID=CustomerID;
           		requestOrder.OrderType=1;//客户自己下单
           	}            
            
            requestOrder.Remarks=$("#remarks").val();
            if($("#remarks").val()==""){
            	toast(_("请填写备注！"));
            }else{
            	if(TYPE==1){//裸钻
            		requestOrder.InventoryIDs=confirmId;
            		if(PlaceOrder==true){
	            		var requestUrl="/Team/SubmitOrder";
	            	}else{
	            		var requestUrl="/Customer/SubmitOrder";
	            	}
	            }else if(TYPE==2){//统包
					if(PlaceOrder==false){
						if(cart_id ==2){
							requestOrder.CartIDs=confirmId;
						}else{
							requestOrder.ProductIDs=confirmId;
						}
					}else{
						requestOrder.ProductIDs=confirmId;
					}
					requestOrder.GoodsType=2;
					requestOrder.InventoryCount=PARAMS.couNum;
	            	var requestUrl="/Team/SubmitOrderNew";
	            }else if(TYPE==3){//成品
					if(PlaceOrder==false){
						if(cart_id ==3){
							requestOrder.CartIDs=confirmId;
						}else{
							requestOrder.ProductIDs=confirmId;
						}
					}else{
						requestOrder.ProductIDs=confirmId;
					}
					requestOrder.GoodsType=3;
	            	requestOrder.IsStock=PARAMS.IsStock;
	            	var requestUrl="/Team/SubmitOrderNew";
	            }else if(TYPE==4){//彩宝
					if(PlaceOrder==false){
						if(cart_id ==2){
							requestOrder.CartIDs=confirmId;
						}else{
							requestOrder.ProductIDs=confirmId;
						}
					}else{
						requestOrder.ProductIDs=confirmId;
					}
                    requestOrder.GoodsType=4;
	            	var requestUrl="/Team/SubmitOrderNew";
	            }else if(TYPE==5){//翡翠
					if(PlaceOrder==false){
						if(cart_id ==2){
							requestOrder.CartIDs=confirmId;
						}else{
							requestOrder.ProductIDs=confirmId;
						}
					}else{
						requestOrder.ProductIDs=confirmId;
					}
                    requestOrder.GoodsType=5;
	            	var requestUrl="/Team/SubmitOrderNew";
	            }else if(TYPE==6){//素金
	            	requestOrder.PrimeGoldIDs=confirmId;
                    requestOrder.GoodsType=6;
	            	var requestUrl="/Team/SubmitOrderNew";
	            }else if(TYPE==7){ //空托
                    if(PlaceOrder==false){
                    	if(cart_id ==7){
                    		requestOrder.CartIDs=confirmId;
						}else{
                    		requestOrder.ProductIDs=confirmId;
						}
					}else{
                    	requestOrder.ProductIDs=confirmId;
					}

                    requestOrder.GoodsType=7;
                    var requestUrl="/Team/SubmitOrderNew";
                }
	            $.ajax({
	                url: requestUrl,//提交地址
	                type: "POST",//提交方式
	                data:JSON.stringify(requestOrder),
	                contentType: "application/json; charset=utf-8",
	                timeout: 30000, //超时时间：30秒
	                success: function (result) {
	                	myApp.hidePreloader();
	                    var _result = eval('(' + result + ')');//eval()解析单个json对象	                   
	                    if(_result.msgcode == true){
	                    	var tipWord="";

							if(_result.msginfo.Successful>0){
								tipWord+=_("成功")+_result.msginfo.Successful+"!";
							}
							if(_result.msginfo.Failed>0){
								tipWord+=_("失败")+_result.msginfo.Failed+_(",已下过单");
							}
	                    	if(PlaceOrder==true){
                                $("#tip_word_box").text(_(tipWord));
                                $(".tip-box").show();

	                    	}else{
	                    		$("#tip_word_box").text(_(tipWord));
	                    		$(".tip-box").show();
	                    	}

	                    }else{
                            myApp.alert(_(_result.msginfo));
						}
	
	                },
		            error: function(XMLHttpRequest, textStatus, errorThrown){
						myApp.hidePreloader();//隐藏加载中
						myApp.alert("网络超时，请重试");
					},
	            });
            }
           
   
        },
	}
	conformOrder.init();
})();

function showNewPage(title,url){
	var appJson={};
	appJson.title=title;
	appJson.url=url;	
	YiYi_APP.showNewPage(JSON.stringify(appJson));
}
